package com.atguigu.handler;

import com.atguigu.pojo.Movie;
import com.atguigu.mapper.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 琉
 * @create 2021-09-06 18:43
 */
@Controller
@RequestMapping("/movie")
public class MovieHandler {
    @Autowired
    private MovieService movieServiceImpl;

    //查询全部
//    @RequestMapping("/showList")
    @GetMapping
    public String showList(Model model){
        //1. 调用业务层的方法查询所有的movie
        List<Movie> movieList = movieServiceImpl.getAll();
        //2. 将查询到的所有movie存储到请求域
        model.addAttribute("movieList",movieList);
        //3. 解析Thymeleaf模板显示所有movie
        return "list";
    }

    //根据ID查询更新电影
//    @RequestMapping("/toUpdatePage")
    @GetMapping("/{movieId}")
    public String toUpdatePage(@PathVariable("movieId") String movieId,Model model){
        //1. 根据要修改的电影的id查询到当前电影的信息
        Movie movie = movieServiceImpl.getMovieById(movieId);
        //2. 将当前电影的信息存储到请求域
        model.addAttribute("movie",movie);
        //3. 解析update页面
        return "update";
    }

    //添加数据
//    @RequestMapping("/saveMovie")
    @PostMapping
    public String addMovie(Movie movie){
        movieServiceImpl.saveMovie(movie);
        return "redirect:/movie";
    }

    //更新电影
//    @RequestMapping("/updateMovie")
    @PutMapping
    public String updateMovie(Movie movie){
        movieServiceImpl.updateMovie(movie);
        return "redirect:/movie";
    }

    //删除电影
    @DeleteMapping("/{movieId}")
    public String removeMovie(@PathVariable("movieId") String movieId){
        movieServiceImpl.removeMovieById(movieId);
        return "redirect:/movie";
    }



}
